package com.tepth.busfix.adapter;

import android.content.Context;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;

import com.tepth.busfix.R;
import com.tepth.busfix.adapter.multi.ItemType;
import com.tepth.busfix.adapter.multi.MultipleField;
import com.tepth.busfix.adapter.multi.MultipleItemEntity;
import com.tepth.busfix.adapter.multi.MultipleRecyclerAdapter;
import com.tepth.busfix.adapter.multi.MultipleViewHolder;
import com.tepth.busfix.bean.reponse.TroubleContent;

import java.util.List;

/**
 * @author zhangyu
 * @date 2018/7/2
 */

public class MultipleOperatorAdapter extends MultipleRecyclerAdapter {
    private VectorDrawableCompat iconAdd;
    private VectorDrawableCompat iconMinus;
    private VectorDrawableCompat iconDelete;
    private List<TroubleContent> mSelectList;

    public MultipleOperatorAdapter(Context context,
                                   List<MultipleItemEntity> data,
                                   List<TroubleContent> selectList) {
        super(data);
        iconAdd = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_icon_add, context.getTheme());
        iconMinus = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_ic_minus, context.getTheme());
        iconDelete = VectorDrawableCompat.create(context.getResources(),
                R.drawable.vector_drawable_ic_delete, context.getTheme());
        mSelectList = selectList;
        addItemType(ItemType.WORK, R.layout.item_repair_operator_work);
        addItemType(ItemType.PUNCH, R.layout.item_repair_operator_punch);
        addItemType(ItemType.REMADE, R.layout.item_repair_operator_remade);
    }

    @Override
    protected void convert(final MultipleViewHolder helper, MultipleItemEntity item) {
        final TroubleContent troubleContent = item.getField(MultipleField.ITEM_OBJ);
        switch (item.getItemType()){
            case ItemType.WORK:
                helper.getView(R.id.checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        troubleContent.setChecked(!troubleContent.isChecked());
                        if (mSelectList != null) {
                            if (troubleContent.isChecked()) {
                                mSelectList.add(troubleContent);
                            } else {
                                mSelectList.remove(troubleContent);
                            }
                        }
                    }
                });
                helper.getView(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        troubleContent.setUseCount(troubleContent.getUseCount() + 1);
                        helper.setText(R.id.tv_count, troubleContent.getUseCount() + "");
                    }
                });
                helper.getView(R.id.iv_minus).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (troubleContent.getUseCount() <= 1) {
                            return;
                        }
                        troubleContent.setUseCount(troubleContent.getUseCount() - 1);
                        helper.setText(R.id.tv_count, troubleContent.getUseCount() + "");
                    }
                });
                AppCompatImageView ivAdd = helper.getView(R.id.iv_add);
                ivAdd.setImageDrawable(iconAdd);
                AppCompatImageView ivMinus = helper.getView(R.id.iv_minus);
                ivMinus.setImageDrawable(iconMinus);
                AppCompatImageView ivDelete = helper.getView(R.id.iv_delete);
                ivDelete.setImageDrawable(iconDelete);
                helper.setChecked(R.id.checkbox,troubleContent.isChecked());
                helper.setText(R.id.tv_trouble_des, troubleContent.getTroubleTypeName().replaceAll("自定义录入@@", "附加作业项"));
                helper.setText(R.id.tv_operator, troubleContent.getOperation().getName());
                TroubleContent.Part part = troubleContent.getAccessory() == null ? troubleContent.getComponent() : troubleContent.getAccessory();
                helper.setText(R.id.tv_part, part.getName());
                helper.setText(R.id.tv_count, troubleContent.getUseCount() + "");
                helper.setText(R.id.tv_unit, part.getUnit());
                helper.setText(R.id.tv_person, "无");
                helper.addOnClickListener(R.id.iv_delete);
                helper.addOnClickListener(R.id.tv_add_trouble);
                break;
        }
    }
}
